/*
 * ZENworks Notification Icon
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename Animation.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.novell.zenworks.agent.zicon;

import java.io.File;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;

import com.novell.zenworks.agent.common.Paths;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_ICON;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_RESPONSE;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_TYPE;
import com.novell.zenworks.agent.zbrowser.MessageBoxLauncher;
import com.novell.zenworks.zicon.common.LogUtil;

public class Animation implements Runnable
{
    private ImageData[] data; //images for animation.
    private static int counter = 0;

    public Animation()
    {
        super();
        final ImageLoader loader = new ImageLoader();
        data = loader.load(Paths.RESOURCE_DIR + File.separator + "images" + File.separator + "zicon" + File.separator + "refresh.gif");
    }

    public ImageData getImageData(int i)
    {
        return data[i];
    }
    
    public int getNumberOfImages()
    {
        return data.length;
    }
    
    public int getDelayTime()
    {
        return data[0].delayTime;
    }

    /**
     */
    private synchronized void animateTrayItem()
    {
        try
        {
            if(!ZTrayIcon.getInstance().getVisible())
                return;
            
            if(Animator.getInstance().isStop())
            {
                ZTrayIcon.getInstance().item.setImage(ZTrayIcon.getInstance().image);                    
                return;
            }

            Image image = new Image(ZTrayIcon.getInstance().display, data[counter]);                                    
            ZTrayIcon.getInstance().item.setImage(image);
            image.dispose();
            counter++;

            if(counter >= data.length)
            {
                counter = 0; //reset counter 
            }
            ZTrayIcon.getInstance().getDisplay().timerExec(getDelayTime() * 10, this);
        }

        catch (Exception e)
        {
            try
            {
                LogUtil.LogException(e.getMessage(), e);

                if(e.getMessage() != null)
                {
                    MessageBoxLauncher.ShowMessageBoxHandler(e.getMessage(), ZTrayIcon.rm.getString("PropDlgTitle") , MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
                }
                else
                {
                    MessageBoxLauncher.ShowMessageBoxHandler(ZTrayIcon.rm.getString("RemotingError"), ZTrayIcon.rm.getString("PropDlgTitle") , MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
                }

            } catch (Exception e1)
            {
                LogUtil.LogException(e.getMessage(), e1);
            }
        }          
    }

    public void run()
    { 
        animateTrayItem();        
    }

}
